package com.stox.core.configurer;

import java.util.Collection;

import com.stox.core.dao.FileTemplate;
import com.stox.core.manager.ExchangeManager;
import com.stox.core.model.Exchange;
import com.stox.core.util.Constant;
import com.stox.core.util.StringUtil;
import com.stox.design.ApplicationConfigurer;

public class ExchangeApplicationConfigurer implements ApplicationConfigurer<Void, Void> {

	private ExchangeManager exchangeManager;
	
	@Override
	public int getOrder() {
		return 3;
	}

	@Override
	public Void invoke(Void payload) {
		Collection<Exchange> exchangeList = exchangeManager.findAll();
		if(exchangeList == null || exchangeList.isEmpty()){
			FileTemplate fileTemplate = new FileTemplate();
			//TODO remove src from the path before exporting jar
			//TODO add below path to jar obfuscator's exclusion list
			String path = Constant.userDir+"com/stox/resources/data/exchanges.csv";
			String string = fileTemplate.read(path);
			if(StringUtil.hasText(string)){
				String[] rows = string.split("\n");  //This file was created on windows, thus specific windows line separator
				if(rows != null && rows.length > 0){
					for(String row : rows){
						exchangeManager.persist(Exchange.fromString(row));
					}
				}
			}
		}
		return null;
	}

	public ExchangeManager getExchangeManager() {
		return exchangeManager;
	}

	public void setExchangeManager(ExchangeManager exchangeManager) {
		this.exchangeManager = exchangeManager;
	}

}
